<%@ page language="java" import="java.util.*" language="java" import="com.jspsmart.upload.*,java.sql.*"  pageEncoding="UTF-8"%>
<%@ page language="java" import="java.util.*" %>
<%@ page language="java" import="java.io.*" %>
<%@ page language="java" import="java.awt.*" %>
<%@ page language="java" import="java.awt.image.*" %>
<%@ page language="java" import="com.sun.image.codec.jpeg.*" %>
<%@ page import="GridCommodity.GridCommodityDeal"%>
<html>
  <head>
    <title>保存商品修改的信息</title>
  </head>
  
  <body>
	  <%
	       
	  	   String saveur="";
	  	   //获取图片的路径
	       String path=request.getParameter("path");
	  	   String GridComID=request.getParameter("GridComID");
	  	   out.print("<script>alert('"+GridComID+"')</script>");
  	       String realPath=request.getRealPath("/")+path;  
  	       //out.print("<script>alert('"+path+"')</script>");
		   //不超过2M
		   int fileSizeAllow=2097152;
		   //可以用中文 
		   request.setCharacterEncoding("gbk");
		   // 新建一个SmartUpload对象
		    SmartUpload su = new SmartUpload();
		    GridCommodityDeal GridCommodity=new GridCommodityDeal();
	
		    // 上传初始化
		    su.initialize(pageContext);
		    // 上传文件
		    su.upload();
		    //取得指定位移处的文件对象file
		    com.jspsmart.upload.File file = su.getFiles().getFile(0);
		    
		    if (file.isMissing()){//文件是否存在
		    	saveur=path;
		    }
		    else
		    {
		    	
		    	int i=GridCommodity.DelFile(realPath);
		        //取得上载的文件的文件名
		   		String fileName=file.getFileName();
		   		//取得文件的大小
		        int fileSize = file.getSize();
		        String extName = file.getFileExt().toLowerCase();
		        //取得后缀名
		        // 将上传文件全部保存到指定目录
		        if (fileSize > fileSizeAllow) {			
			   %>
			    <script language=javascript>
			    alert("文件太大了 ");
			    window.location='GridCommodityDeal.jsp';
			    </script>
			    
			   <%}
		        if (!(extName.equals("gif")||extName.equals("jpg") )) {
			   %>
			    <script language=javascript>
			    alert("图片格式不正确  ");
			    window.location='GridCommodityDeal.jsp';
			    </script>
			    
			   <%}
			   String nowTime=new java.util.Date().toLocaleString().replaceAll(":","-").replaceAll(" ","-");
			   String fileNameNew=nowTime+"."+ extName;// 以日期命名的格式
			   file.saveAs("/UpLoadFile/"+fileNameNew, SmartUpload.SAVE_VIRTUAL);
			    saveur="UpLoadFile"+"\\"+fileNameNew;
			    String saveurl=request.getRealPath("/")+saveur;
			    //从跳转前页面获取参数  
		    }
		   
		    GridCommodity.setComID(GridComID);
	        GridCommodity.setComName(su.getRequest().getParameter("ComName2"));
			GridCommodity.setConKind((String)su.getRequest().getParameter("ConKind2"));
			GridCommodity.setDescribe((String)su.getRequest().getParameter("Describe2"));
			GridCommodity.setRemCount(Integer.parseInt(su.getRequest().getParameter("ComNum2")));
			GridCommodity.setComPrice((float)Double.parseDouble(su.getRequest().getParameter("ComPrice2")));
			GridCommodity.setComInf(su.getRequest().getParameter("ComInf2"));
			GridCommodity.setComImage(saveur);
			GridCommodity.setRecency(Integer.parseInt(su.getRequest().getParameter("Recency")));
		    //out.print("<script>alert('"+saveurl+"')</script>");
		    if(GridCommodity.UpdateGridCommodity(GridCommodity.getComID(),GridCommodity.getComName(),GridCommodity.getConKind(),GridCommodity.getDescribe(),GridCommodity.getComImage(),
		    		GridCommodity.getRemCount(),GridCommodity.getComPrice(),GridCommodity.getComInf(),GridCommodity.getRecency())==0)
			{
		       
			   out.print("<script>alert('修改商品信息失败 ，请重新尝试  ')</script>");
		    }
		    else
		    {
		    	out.print("<script>alert('"+GridCommodity.getComID()+"')</script>");
		    	out.print("<script>alert('"+GridCommodity.getComName()+"')</script>");
		    	out.print("<script>alert('"+GridCommodity.getConKind()+"')</script>");
		    	out.print("<script>alert('"+GridCommodity.getComImage()+"')</script>");
		       out.print("<script>alert('"+GridCommodity.getDescribe()+"')</script>");
		       out.print("<script>alert('"+GridCommodity.getComInf()+"')</script>");
			   out.print("<script>alert('修改商品信息成功')</script>");
			   
		    }
	%>
	   <script type="text/javascript">
	
	   window.location.href="GridCommodityDeal.jsp";    
	
	  </script>
  </body>
</html>
